package android.se1;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public  class ResetPasswordActivity extends Activity {

	private EditText pw, repw;
	private Button resetPassword;
	int match;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork() // or .detectAll() for all detectable problems
		.penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resetpasword);
		//final String email_address = "";
		
		pw = (EditText) findViewById(R.id.editText3);
		repw = (EditText) findViewById(R.id.editText4);
		resetPassword = (Button) findViewById(R.id.buttonChangePass);
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		//SharedPreferences.Editor editor = settings.edit();
		final String email_address=settings.getString("email",null );

		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

		resetPassword.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub

				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

				String password = pw.getText().toString();
				String retype_password = repw.getText().toString();

				if (password.equals(retype_password)) {
					match = 0;
				} else {
					match = -1;
				}
				
				if(pw.getText().toString().length()<6)
				{
					
					alertDialog
							.setMessage("Password should be atleast 6 characters. Please type again.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}				
				else if (match == -1) {
					alertDialog.setTitle("Reset Password");
					alertDialog
							.setMessage("Password does not match. Please retype password.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				} else {
					postParameters.add(new BasicNameValuePair("email", email_address));
					postParameters.add(new BasicNameValuePair("pw", pw.getText().toString()));
					
					String response = null;

					try {

						response = CustomHttpClient.executeHttpPost(
								//"http://192.168.1.145/CSE5324/resetpassword.php",
								//"http://129.107.149.180/CSE5324/resetpassword.php",
								"http://omega.uta.edu/~nxk0518/CSE5324/resetpassword.php",
								//"http://192.168.0.3/mavbulletin/resetpassword.php",
								postParameters);

						String res = response.toString();

						res = res.replaceAll("\\s+", "");

						if (res.equals("1")) {
							System.out.println("Result =" + res);
							// error.setText("Registration Successful");
							alertDialog.setTitle("Reset Password");
							alertDialog.setMessage("Password Changed Successfully. Please click Ok to Sign In.");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											// here you can add functions
											Intent signinScreen = new Intent(
													getApplicationContext(),
													MavBulletinActivity.class);
											startActivity(signinScreen);
										}
									});

							alertDialog.show();

						} else {
							System.out.println("Result =" + res);
							System.out.println("Email =" + email_address);
							System.out.println("Password =" + pw.getText().toString());
							
							Log.e("log_tag",
									"Email =" + email_address);
							// error.setText("Error in Saving Information!");
							alertDialog.setTitle("Reset Password");
							alertDialog
									.setMessage("Error in Resetting Password!");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int type) {
											// here you can add functions
										}
									});

							alertDialog.show();
						}
					} catch (Exception e) {
						Log.e("log_tag",
								"Error in http connection!!" + e.toString());
						//email.setText(e.toString());
					}

				}

			}

		});

	}
}